from PyQt6.QtWidgets import (
    QWidget, QVBoxLayout, QPushButton, QLabel, QLineEdit, 
    QComboBox, QGroupBox
)
from PyQt6.QtGui import QFont
from PyQt6.QtCore import Qt, QTimer

class SignalsTab:
    """信号与槽标签页"""
    
    def __init__(self, status_bar=None):
        self.status_bar = status_bar
        
    def create_tab(self):
        """创建信号与槽标签页"""
        tab = QWidget()
        layout = QVBoxLayout(tab)
        
        title = QLabel("信号与槽机制")
        title.setFont(QFont("Arial", 16, QFont.Weight.Bold))
        title.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(title)
        
        # 按钮点击示例
        btn_click_group = QGroupBox("按钮点击事件")
        btn_click_layout = QVBoxLayout(btn_click_group)
        
        self.click_label = QLabel("点击按钮测试信号连接")
        self.click_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        
        click_btn = QPushButton("点击我!")
        click_btn.clicked.connect(self.on_button_click)
        
        btn_click_layout.addWidget(self.click_label)
        btn_click_layout.addWidget(click_btn)
        layout.addWidget(btn_click_group)
        
        # 文本改变示例
        text_change_group = QGroupBox("文本改变事件")
        text_change_layout = QVBoxLayout(text_change_group)
        
        self.text_input = QLineEdit()
        self.text_input.setPlaceholderText("在此输入文本...")
        self.text_input.textChanged.connect(self.on_text_changed)
        
        self.text_output = QLabel("输入内容将显示在这里")
        self.text_output.setAlignment(Qt.AlignmentFlag.AlignCenter)
        
        text_change_layout.addWidget(self.text_input)
        text_change_layout.addWidget(self.text_output)
        layout.addWidget(text_change_group)
        
        # 下拉框选择示例
        combo_group = QGroupBox("下拉框选择事件")
        combo_layout = QVBoxLayout(combo_group)
        
        self.combo_box = QComboBox()
        self.combo_box.addItems(["红色", "绿色", "蓝色", "黄色", "紫色"])
        self.combo_box.currentIndexChanged.connect(self.on_combo_changed)
        
        self.color_display = QLabel("颜色预览")
        self.color_display.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.color_display.setStyleSheet("background-color: #ff0000; color: white;")
        self.color_display.setMinimumHeight(60)
        
        combo_layout.addWidget(self.combo_box)
        combo_layout.addWidget(self.color_display)
        layout.addWidget(combo_group)
        
        return tab
    
    def on_button_click(self):
        """按钮点击事件处理"""
        self.click_label.setText("按钮被点击了!")
        self.click_label.setStyleSheet("color: green; font-weight: bold;")
        QTimer.singleShot(2000, lambda: self.click_label.setText("点击按钮测试信号连接"))
        QTimer.singleShot(2000, lambda: self.click_label.setStyleSheet(""))
    
    def on_text_changed(self, text):
        """文本改变事件处理"""
        self.text_output.setText(f"你输入了: {text}")
    
    def on_combo_changed(self, index):
        """下拉框选择改变事件处理"""
        colors = {
            0: ("红色", "#ff0000"),
            1: ("绿色", "#00ff00"),
            2: ("蓝色", "#0000ff"),
            3: ("黄色", "#ffff00"),
            4: ("紫色", "#800080")
        }
        name, code = colors.get(index, ("红色", "#ff0000"))
        self.color_display.setText(name)
        self.color_display.setStyleSheet(f"background-color: {code}; color: white;")